package com.laozhao.wintersystem;

import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.swing.Icon;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import free.FreeMenu;
import free.FreeMenuItem;

public class WMenubarModel {

	private final String ELEMENT_MENUBAR_MENU = "menu";
	private final String ELEMENT_MENUBAR_MENUITEM = "menuitem";

	private final String ATTLIST_MENU_NAME = "name";
	private final String ATTLIST_MENU_ICON = "icon";
	private final String ATTLIST_MENU_TIP = "tip";

	private final String ATTLIST_MENUITEM_NAME = "name";
	private final String ATTLIST_MENUITEM_ICON = "icon";
	private final String ATTLIST_MENUITEM_TIP = "tip";
	private final String ATTLIST_MENUITEM_LISTENER = "listener";
	private final String ATTLIST_MENUITEM_COMMAND = "command";

	private JMenuBar menuBar = new JMenuBar();
	private Map<JMenuItem, String> itemsMap = new HashMap<JMenuItem, String>();

	String tmpName;
	Icon tmpIcon;
	String tmpTip;
	String tmpListener;
	String tmpCommand;

	public WMenubarModel(Node node) {
		load(node);
	}

	public JMenuBar getMenuBar() {
		return menuBar;
	}

	public void addListenerModel(WListenersModel lModel) {
		if (!itemsMap.isEmpty()) {
			for (Map.Entry<JMenuItem, String> entry : itemsMap.entrySet()) {
				entry.getKey().addActionListener(lModel.getListener(entry.getValue()));
			}
		}
	}
	
	public void addActionListener(ActionListener l){
		if (!itemsMap.isEmpty()) {
			for (JMenuItem menuItem : itemsMap.keySet()) {
				menuItem.addActionListener(l);
			}
		}
	}

	private void load(Node node) {
		if (node != null) {
			// <!ELEMENT menubar(nenu+,nenuitem?)>
			NodeList menuNodes = node.getChildNodes();
			for (int k = 0; k < menuNodes.getLength(); k++) {
				Node menu = menuNodes.item(k);
				if (menu.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE 
						&& node.getNodeName().equalsIgnoreCase(ELEMENT_MENUBAR_MENU)) {
					tmpName = WSTools.getStringAttribute(menu,
							ATTLIST_MENU_NAME);
					tmpIcon = WSTools.getIconAttribute(menu,
							ATTLIST_MENU_ICON);
					tmpTip = WSTools.getStringAttribute(menu,
							ATTLIST_MENU_TIP);
					JMenu rootMenu = new JMenu(tmpName);
					rootMenu.setIcon(tmpIcon);
					rootMenu.setToolTipText(tmpTip);
					menuBar.add(rootMenu);
					createMenu(rootMenu, menu);
					
				}
				// map.put(tmpId, new WListener(tmpClass, unique));
			}
		}
	}

	private void createMenu(JMenu rootMenu, Node node) {
		if (node != null) {
			NodeList menus = node.getChildNodes();
			for (int k = 0; k < menus.getLength(); k++) {
				Node menu = menus.item(k);
				if (menu.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
					boolean isMenuItem = menu.getNodeName().equalsIgnoreCase(
							"menuitem");
					boolean isMenu = menu.getNodeName()
							.equalsIgnoreCase("menu");
					if (isMenu) {
						tmpName = WSTools.getStringAttribute(menu,
								ATTLIST_MENU_NAME);
						tmpIcon = WSTools.getIconAttribute(menu,
								ATTLIST_MENU_ICON);
						tmpTip = WSTools.getStringAttribute(menu,
								ATTLIST_MENU_TIP);
						JMenu jMenu = new JMenu(tmpName);
						jMenu.setIcon(tmpIcon);
						jMenu.setToolTipText(tmpTip);

						rootMenu.add(jMenu);
						createMenu(jMenu, menu);
					}
					if (isMenuItem) {
						tmpName = WSTools.getStringAttribute(menu,
								ATTLIST_MENUITEM_NAME);
						tmpIcon = WSTools.getIconAttribute(menu,
								ATTLIST_MENUITEM_ICON);
						tmpTip = WSTools.getStringAttribute(menu,
								ATTLIST_MENUITEM_TIP);
						tmpListener = WSTools.getStringAttribute(menu,
								ATTLIST_MENUITEM_LISTENER);
						tmpCommand = WSTools.getStringAttribute(menu,
								ATTLIST_MENUITEM_COMMAND);
						JMenuItem menuItem = new JMenuItem(tmpName);

						// menuItem.setText(text);
						menuItem.setToolTipText(tmpTip);
						menuItem.setActionCommand(tmpCommand);
						menuItem.setIcon(tmpIcon);
						// menuItem.
						rootMenu.add(menuItem);
						if (tmpListener == null || "".equals(tmpListener)) {
							tmpListener = WListenersModel.DEFAULT_MENU_LISTENER_ID;
						}
						itemsMap.put(menuItem, tmpListener);
					}
				}
			}
		}
	}

}
